<?php
class Admin_AdpackageController extends Zendhlt_Controller_AdminAction {

	const ADD_PAGE_TITLE = 'Thêm gói cước';
	const LIST_PAGE_TITLE = 'Danh sách gói cước';
	const EDIT_PAGE_TITLE = 'Chỉnh sửa gói cước';
	const ACTIVE = 1;
	
	private $_currentUser;
	private $_arrParam;
	private $_paginator = array();
	private $_actionMain;
	private $_namespace;
	
	public function init() {
		$layoutPath = TEMPLATE_PATH . '/admin';
		$option = array('layout'     => 'index',
                   		'layoutPath' => $layoutPath);
		Zend_Layout::startMvc($option);
		
		// Phân trang
		$this->_paginator['itemCountPerPage'] = 5;
		$this->_paginator['pageRange'] 		  = 3;
		$this->_paginator['currentPage'] 	  = $this->_request->getParam('page', 1);
		
		// Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		// Duong dan cua Action chinh
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
		
		// Dat ten SESSION
		// Luu cac du lieu filter vao SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		
		if(empty($ssFilter->col) || ($this->_request->getParam('resetfilter', 0) == 1)) {
			$ssFilter->keywords 	= '';
			$ssFilter->col 			= 'ngay_tao';
			$ssFilter->order 		= 'DESC';
			$ssFilter->state 		= self::ACTIVE;
			$ssFilter->error 		= 0;
		}
		
		$this->_arrParam['paginator']			 = $this->_paginator;
		$this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
		$this->_arrParam['ssFilter']['col'] 	 = $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 	 = $ssFilter->order;
		$this->_arrParam['ssFilter']['state'] 	 = $ssFilter->state;
		
		// Truyen ra view
		$this->view->actionMain = $this->_actionMain;
		$this->view->currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
		$this->view->arrParam = $this->_arrParam;
		
		// Lay thong tin user admin hien tai
		$auth = Zend_Auth::getInstance();
		$this->_currentUser = $auth->getIdentity();
	}
	
	public function indexAction() {
		$this->view->headTitle(self::LIST_PAGE_TITLE);
		
		$model = new Admin_Model_AdpackageMapper();
		$paginator = new Zendhlt_Paginator();

		$packages = $model->fetch($this->_arrParam);
		$totalItem = $model->countItem($this->_arrParam);

		$this->view->panigator = $paginator->createPaginator($totalItem, $this->_paginator);
		$this->view->totalItems = $totalItem;
		$this->view->Items = $packages;
		
		// Display error if delete occur error {
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		$this->view->msg = '';
		
		if ($ssFilter->error == 1) {
			$this->view->msg = 'Xuất hiện lổi trong quá trình thực hiện!';
			$ssFilter->error = 0;
		}
		// }
	}
	
	public function addAction() {
		$this->view->headTitle(self::ADD_PAGE_TITLE);

		$form = new Admin_Form_Adpackage();
		$this->view->form = $form;
		$this->view->msg = '';

		$model = new Admin_Model_AdpackageMapper();
		
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();

			if ($form->isValid($formData)) {
				$currentDate = date('Y-m-d H:i:s');
				$package = new Admin_Model_TableColumn_Adpackage();
				$package->setTen_goi($form->getValue('ten_goi'));
				$package->setGia($form->getValue('gia'));
				$package->setMo_ta($form->getValue('mo_ta'));
				$package->setNguoi_tao($this->_currentUser->id);
				$package->setNgay_tao($currentDate);
				$package->setNguoi_cap_nhat($this->_currentUser->id);
				$package->setNgay_cap_nhat($currentDate);
				$package->setTrang_thai(self::ACTIVE);
				
				if ($model->save($package)) {
					$form->reset();
				} else {
					$this->view->msg = 'Xuất hiện lỗi trong quá trình thực hiện';
					$form->populate($formData);
				}
			} else {
				$form->populate($formData);
			}
		}
	}

	public function filterAction() {
		$ssFilter = new Zend_Session_Namespace($this->_namespace);

		if ($this->_arrParam['type'] == 'search') {
			if($this->_arrParam['key'] == 1) {
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
				$ssFilter->state = $this->_arrParam['state'];
			} else {
				$ssFilter->keywords = '';
				$ssFilter->state = self::ACTIVE;
			}
		}
		
		if ($this->_arrParam['type'] == 'order') {
			$ssFilter->col = $this->_arrParam['col'];
			$ssFilter->order = $this->_arrParam['by'];
		}
		
		$this->_redirect($this->_actionMain . '/index');
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function editAction() {
		$this->view->headTitle(self::EDIT_PAGE_TITLE);
		
		$form = new Admin_Form_Adpackage();
		$this->view->form = $form;
		$this->view->msg = '';
		
		$model = new Admin_Model_AdpackageMapper();
		$arrData = $model->fetchAll('id=' . $this->_getParam('id', 0), 'edit');
		$data = array_shift($arrData);
		
		if ($data != null) {
			if ($this->getRequest()->isPost()) {
				$formData = $this->getRequest()->getPost();
			
				if ($form->isValid($formData)) {
					$currentDate = date('Y-m-d H:i:s');
					$package = new Admin_Model_TableColumn_Adpackage($data);
					$package->setTen_goi($form->getValue('ten_goi'));
					$package->setGia($form->getValue('gia'));
					$package->setMo_ta($form->getValue('mo_ta'));
					$package->setNguoi_cap_nhat($this->_currentUser->id);
					$package->setNgay_cap_nhat($currentDate);
					
					if ($model->save($package)) {
						$this->_redirect($this->_actionMain . '/index/page/' . $this->_paginator['currentPage']);
					} else {
						$this->view->msg = 'Xuất hiện lổi trong quá trình thực hiện.';
						$form->populate($formData);
					}
				} else {
					$form->populate($formData);
				}
			} else {
				$form->populate($data);
			}
		} else {
			$this->view->msg = 'Dữ liệu không tồn tại trong database.';
		}
	}

	public function deleteAction() {
		$model = new Admin_Model_AdpackageMapper();
		
		if (isset($this->_arrParam['id'])) {
			$id = $this->_arrParam['id'];
		} elseif (isset($this->_arrParam['cid'])) {
			$id = $this->_arrParam['cid'];
		}
		
		if ($id != null) {
			$state = $this->_arrParam['delete-type'];
			
			if ($model->deleteItems($id, $state, $this->_currentUser->id) == false) {
				$ssFilter = new Zend_Session_Namespace($this->_namespace);
				$ssFilter->error = 1;
			}
		}
			
		$this->_redirect($this->_actionMain . '/index');
		$this->_helper->viewRenderer->setNoRender();
	}
	
}
?>